package com.gitee.wsl.android.ui.ext.type

import android.graphics.DashPathEffect
import android.graphics.PathEffect
import android.graphics.PointF
import com.gitee.wsl.android.ext.math.center
import com.gitee.wsl.ext.math.Dimensions
import com.gitee.wsl.android.ext.math.distance
import com.gitee.wsl.android.ext.math.percent
import com.gitee.wsl.android.ext.math.translate

object LineExt {
    //点线
    val dotLineStyle: PathEffect by lazy {
        DashPathEffect(floatArrayOf(2f, 2f, 2f, 2f), 1f)
    }

    //虚实线
    val dashLineStyle: PathEffect by lazy {
        DashPathEffect(floatArrayOf(4f, 8f, 5f, 10f), 1f)
    }

    /**
     * B(t) = (1 - t)^2 * P0 + 2t * (1 - t) * P1 + t^2 * P2, t ∈ [0,1]
     *
     * @param t  曲线长度比例
     * @param p0 起始点
     * @param p1 控制点
     * @param p2 终止点
     * @return t对应的点
     */
    fun calculateBezierPointForQuadratic(t: Float, p0: PointF, p1: PointF, p2: PointF): PointF {
        val point = PointF()
        val temp = 1 - t
        point.x = temp * temp * p0.x + 2 * t * temp * p1.x + t * t * p2.x
        point.y = temp * temp * p0.y + 2 * t * temp * p1.y + t * t * p2.y
        return point
    }

    /**
     * B(t) = P0 * (1-t)^3 + 3 * P1 * t * (1-t)^2 + 3 * P2 * t^2 * (1-t) + P3 * t^3, t ∈ [0,1]
     *
     * @param t  曲线长度比例
     * @param p0 起始点
     * @param p1 控制点1
     * @param p2 控制点2
     * @param p3 终止点
     * @return t对应的点
     */
    fun calculateBezierPointForCubic(
        t: Float,
        p0: PointF,
        p1: PointF,
        p2: PointF,
        p3: PointF
    ): PointF {
        val point = PointF()
        val temp = 1 - t
        point.x = p0.x * temp * temp * temp + 3 * p1.x * t * temp * temp + 3 * p2.x * t * t * temp + p3.x * t * t * t
        point.y = p0.y * temp * temp * temp + 3 * p1.y * t * temp * temp + 3 * p2.y * t * t * temp + p3.y * t * t * t
        return point
    }

    // 求A点到B点的三次贝塞尔曲线的两个控制点
    fun curve3(a: PointF, b: PointF, l: PointF, n: PointF) : Pair<PointF,PointF>{
        // 点a前一个点和点a的中点
        val cLA: PointF = Dimensions.center(l, a)
        // 点a和点b的中点
        val cAB: PointF = Dimensions.center(a, b)
        // 点b和点b后一个点的中点
        val cBN: PointF = Dimensions.center(b, n)

        // 点a前一个点到点a的距离
        val lenLA: Float = Dimensions.distance(l, a)
        // 点a到点b的距离
        val lenAB: Float = Dimensions.distance(a, b)
        // 点b到点b后一个点的距离
        val lenBN: Float = Dimensions.distance(b, n)

        // cLA和cAB连线的比例点
        val cLAB: PointF = Dimensions.percent(cLA, cAB, lenLA / (lenLA + lenAB))
        val cABN: PointF = Dimensions.percent(cAB, cBN, lenAB / (lenAB + lenBN))

        // 顶点a和顶点b的控制点1
        val ctrlPoint1 = Dimensions.translate(cAB, a.x - cLAB.x, a.y - cLAB.y)
        // 顶点a和顶点b的控制点2
        val ctrlPoint2 = Dimensions.translate(cAB, b.x - cABN.x, b.y - cABN.y)

        return ctrlPoint1 to ctrlPoint2
    }
}


